﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class UserControls_AdminPagingListing : System.Web.UI.UserControl
{
    private int _perPage = 18;

    public int PerPage
    {
        get { return _perPage; }
        set { _perPage = value; }
    }
    private int _curPage = 1;

    public int CurPage
    {
        get { return _curPage; }
        set { _curPage = value; }
    }

    private int _total;

    public int Total
    {
        get { return _total; }
        set { _total = value; }
    }
    public int TotalPage;
    public string first;
    public string latest;
    public string pre;
    public string next;
    public string before;
    public string after;

    protected void Page_Load(object sender, EventArgs e)
    {
        double temp = (double)Total / PerPage;
        TotalPage = (temp - (int)(Total / PerPage)) > 0 ? ((int)(Total / PerPage) + 1) : ((int)temp);

        string url = "";
        #region Check current page
        if (Request["p"] != null)
            CurPage = int.Parse(Request["p"]);
        else
            CurPage = 1;
        url = Request.RawUrl.Substring(0, Request.RawUrl.IndexOf("aspx") + 4) + "?";
        for (int i = 0; i < Request.QueryString.AllKeys.Length; i++)
        {
            if (Request.QueryString.AllKeys[i] != "p")
            {
                if (i > 0)
                    url += "&";
                url += Request.QueryString.AllKeys[i] + "=" + Request.QueryString.Get(i);
            }
        }
        url += "&p=";
        #endregion

        #region Latest
        if (CurPage == TotalPage || TotalPage==0)
        {
            latest = "<li class=\"next-pre\"><img src=\"" + ResolveUrl("~/images/nav_nextset_off.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></li>";
        }
        else
        {
            latest = "<li class=\"next-pre\"><a href=\"" + url + TotalPage.ToString() + "\"><img src=\"" + ResolveUrl("~/images/nav_nextset_on.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></a></li>";
        }
        #endregion

        #region First
        if (CurPage == 1)
        {
            first = "<li class=\"next-pre\"><img src=\"" + ResolveUrl("~/images/nav_previousset_off.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></li>";
        }
        else
        {
            first = "<li class=\"next-pre\"><a href=\"" + url + "1" + "\"><img src=\"" + ResolveUrl("~/images/nav_previousset_on.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></a></li>";
        }
        #endregion

        #region preview
        if (CurPage <= 1)
        {
            pre = "<li class=\"next-pre\"><img src=\"" + ResolveUrl("~/images/nav_previouspage_off.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></li>";
        }
        else
        {
            pre = "<li class=\"next-pre\"><a href=\"" + url + (CurPage - 1).ToString() + "\"><img src=\"" + ResolveUrl("~/images/nav_previouspage_on.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></a></li>";
        }
        #endregion

        #region next
        if (CurPage >= TotalPage)
        {
            next = "<li class=\"next-pre\"><img src=\"" + ResolveUrl("~/images/nav_nextpage_off.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></li>";
        }
        else
        {
            next = "<li class=\"next-pre\"><a href=\"" + url + (CurPage + 1).ToString() + "\"><img src=\"" + ResolveUrl("~/images/nav_nextpage_on.gif") + "\" alt=\"thoi trang\" width=\"28\" height=\"17\" /></a></li>";
        }
        #endregion

        #region Before;
        before = "";
        for (int i = CurPage - 3; i < CurPage; i++)
        {
            if(i>0)
                before += "<li class=\"go\"><a href=\"" + url + i.ToString() + "\">" + i.ToString() + "</a></li>\n\t\t\t\t\t\t\t";
        }
        #endregion

        #region After;
        after = "";
        for (int i = CurPage + 1; i < CurPage + 4 && i <= TotalPage; i++)
        {
            after += "<li class=\"go\"><a href=\"" + url + i.ToString() + "\">" + i.ToString() + "</a></li>\n\t\t\t\t\t\t\t";
        }
        after += "<li style=\"width:0px;\"></li>\n\t\t\t\t\t\t\t";
        #endregion
    }
}
